<?php

$CFetchers->fetchModelId($Db,
                         $lstHeader["MODEL"][0],
                         $SourceDb->fetchValue("srcDbModel"),
                         $modelId);

$CFetchers->fetchIsSerNumber($Db,
                             $lstHeader["MODEL"][0],
                             $modelId,
                             $SourceDb->fetchValue("srcDbModel"),
                             $isSerNumber);

/**
 * Only process if isSN
 */
if($isSerNumber == 1){

  if($isApprove){

    for($iRow = 0; $iRow < $nDetail ; $iRow++){

      $values = " '".$jobId."',     ".
                "  ".($iRow+1)." ,  ".
                "  ".$modelId." ,   ".
                "  1 ,    ".
                "  ".$lstDetail["HEAD"][$iRow]." ,   ".
                "  ".$lstDetail["LENGTH"][$iRow]." , ".
                " '".$lstDetail["SER_NUM"][$iRow]."'  ";

      $CHandlers->insToSnDtl($Db,
                             $values,
                             $SourceDb->fetchValue("srcDbSnDtl"));

      /**
       * since this is a SN
       * insert into SN detail table
       */
      $values = " '".$jobId."',     ".
                "  ".($iRow+1)." ,  ".
                "  1 ,              ".
                " '".$lstDetail["SER_NUM"][$iRow]."',   ".
                " '".$lstHeader["WAREHOUSE"][$iRow]."', ".
                "  "._OPNAME_PLUS."                     ";

      $CHandlers->insToSN($Db,
                          $values,
                          $SourceDb->fetchValue("srcDbSN"));
    }
  }else{

    for($iRow = 0; $iRow < $nDetail ; $iRow++){

      /**
       * perform isSnClean
       */
      $CFetchers->fetchIsSnClean($Db,
                                 $lstDetail["SER_NUM"][$iRow],
                                 _OPNAME_PLUS,
                                 $SourceDb->fetchValue("srcDbSN"),
                                 $isClean);

      if(!$isClean){

        $Misc->usrDieJs("Error: SN is not clean or being used, ".$lstDetail["SER_NUM"][$iRow]." ");
      }
    }

    $CHandlers->dbEntDelete($Db,
                            $jobId,
                            $SourceDb->fetchValue("srcDbSnDtl"));

    $CHandlers->dbEntDelete($Db,
                            $jobId,
                            $SourceDb->fetchValue("srcDbSN"));
  }
}else{

  /* Do nothing */
}
?>